package com.monkey.core.web.response;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.HashMap;
import java.util.List;

/**
 * Created by silentwu on 2015/7/3.
 */
public class MonkeyResponseDto extends HashMap<String, Object> {
    public static final String SUCCESSFUL_KEY = "success";
    public static final String RESPONSE_CODE_KEY = "responseCode";
    public static final String MESSAGE_KEY = "message";
    public static final String DEFAULT_DATA_KEY = "data";

    private String dataKey = DEFAULT_DATA_KEY;

    public MonkeyResponseDto() {
    }


    public MonkeyResponseDto(String dataKey) {
        this.dataKey = dataKey;
    }

    public Object getData() {
        return this.get(this.dataKey);
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T setData(Object data) {
        this.put(this.dataKey, data);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success(String message) {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_SUCCESS);
        this.put(MESSAGE_KEY, message);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success() {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_SUCCESS);
        this.put(MESSAGE_KEY, "操作成功");
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success(Object data) {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_SUCCESS);
        this.put(MESSAGE_KEY, "操作成功");
        this.put(dataKey, data);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success(String message, Object data) {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_SUCCESS);
        this.put(MESSAGE_KEY, message);
        this.put(dataKey, data);
        return (T) this;
    }


    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success(int responseCode, String message) {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, responseCode);
        this.put(MESSAGE_KEY, message);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T success(int responseCode, String message, Object data) {
        this.put(SUCCESSFUL_KEY, true);
        this.put(RESPONSE_CODE_KEY, responseCode);
        this.put(MESSAGE_KEY, message);
        this.put(dataKey, data);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T fail(String message) {
        this.put(SUCCESSFUL_KEY, false);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_FAIL);
        this.put(MESSAGE_KEY, message);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T fail(int responseCode, String message) {
        this.put(SUCCESSFUL_KEY, false);
        this.put(RESPONSE_CODE_KEY, responseCode);
        this.put(MESSAGE_KEY, message);
        return (T) this;
    }

    @SuppressWarnings("unchecked")
    public <T extends MonkeyResponseDto> T fail(BindingResult result) {
        String message = "";
        final List<FieldError> fieldErrors = result.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            final String defaultMessage = fieldError.getDefaultMessage();
            message += defaultMessage + ";";
        }
        this.put(SUCCESSFUL_KEY, false);
        this.put(RESPONSE_CODE_KEY, ResponseCode.OPERATE_FAIL);
        this.put(MESSAGE_KEY, message);
        return (T) this;
    }

    public String getDataKey() {
        return dataKey;
    }

    public void setDataKey(String dataKey) {
        this.dataKey = dataKey;
    }
}
